package com.example.study.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.study.common.api.ResultUtil;
import com.example.study.mapper.CourseMapper;
import com.example.study.model.CourseDatePO;
import com.example.study.model.CourseDateVO;
import com.example.study.model.CoursePO;
import com.example.study.model.CourseVO;
import com.example.study.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CoursePO> implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Override
    public ResultUtil createCourse(CourseVO courseVO) {
        log.info("create Course begin, receive params: {}", courseVO.toString());
        CoursePO coursePO = new CoursePO();
        BeanUtils.copyProperties(courseVO, coursePO);
        int row = courseMapper.insert(coursePO);
        if (row > 0){
            log.info("create course success! result: {}", coursePO.toString());
            return ResultUtil.success(coursePO);
        }
        log.error("create course failed!");
        return ResultUtil.failed("创建课程失败");
    }

    @Override
    public ResultUtil createCourseDate(CourseDateVO courseDateVO) {
        log.info("create course date begin, receive params: {}", courseDateVO.toString());
        CourseDatePO courseDatePO = new CourseDatePO();
        BeanUtils.copyProperties(courseDateVO, courseDatePO);
        int row = courseMapper.insertCourseDate(courseDatePO);
        if (row > 0){
            log.info("create course date success! result: {}", courseDatePO.toString());
            return ResultUtil.success(courseDatePO);
        }
        log.error("create course date failed!");
        return ResultUtil.failed("创建课程失败");
    }
}
